package com.wfs.myapp.controller.bk;

import com.wfs.myapp.common.ApiResult;
import com.wfs.myapp.common.Constant;
import com.wfs.myapp.entity.dto.req.bk.AddDishReqDto;
import com.wfs.myapp.service.BkDishService;
import com.wfs.myapp.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/bk/dish")
@Api(value = "书籍", tags = "书籍")
@Slf4j
public class DishController {

    @Autowired
    private BkDishService bkDishService;

    @PostMapping("/addOrUpdate")
    @ApiOperation(value = "新建/修改")
    public ApiResult<?> addOrUpdate(@Valid @RequestBody AddDishReqDto addDishReqDto, HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return bkDishService.addOrUpdate(userId, addDishReqDto);
    }

    @PostMapping("/remove")
    @ApiOperation(value = "删除")
    public ApiResult<?> remove(@Valid @RequestBody AddDishReqDto addDishReqDto, HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return bkDishService.remove(userId, addDishReqDto);
    }

    @GetMapping("/dishList")
    @ApiOperation(value = "列表")
    public ApiResult<?> dishList(@RequestParam("page") Integer page,
                                    @RequestParam("size") Integer size,
                                    HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return bkDishService.dishList(userId, page, size);
    }

    @GetMapping("/dishInfo")
    @ApiOperation(value = "详情")
    public ApiResult<?> bookInfo(@RequestParam("id") Long id) {
        return bkDishService.dishInfo(id);
    }

    @GetMapping("/dishInfoByName")
    @ApiOperation(value = "详情")
    public ApiResult<?> bookInfoByName(@RequestParam("name") String name, @RequestParam("page") Integer page, @RequestParam("size") Integer size) {
        return bkDishService.dishInfoByName(name, page, size);
    }

}
